<?php
namespace YzPay\Hooks;
class YzPayUserHooks extends \HooksController {
	function member_load(&$param) {
		$user_id = $param['user_id'];
		$data = BoxModel("yz_member")->where(array('user_id' => $user_id))->find();
		if (is_array($data)) {
			$param = array_merge($param, $data);
		}
		$param['fee_rate'] = $param['fee_rate'] > 0 ? $param['fee_rate'] : dict('system_fee_rate');
	}
//扫场景码
	function wxScan_yzpay(&$param) {
		$this->store_id = getStoreID();
		$this->storeInfo = BoxModel("addon_wx_config")->load($this->store_id);
		//dump($this->storeInfo);
		$config = array(
			'token' => $this->storeInfo['token'],
			'appID' => $this->storeInfo['appID'],
			'appsecret' => $this->storeInfo['appsecret'],
		);
		$this->wechatObj = new \Common\Org\weiXin($config);
		$openid = $param['openid'];
		$keyName = intval($param['keyName']);
		$user_info = BoxModel("system_member")->where(array('openid' => $openid))->find();
		//扫个人二维码
		$p_user_info = BoxModel("system_member")->where(array('user_id' => $keyName))->find();
		//$this->wechatObj->sendText($openid, $user_info['user_id']);
		//$this->wechatObj->sendText($openid, "keyName:" . $keyName);
		if (is_array($p_user_info)) {
			//$this->wechatObj->sendText($openid, "has user_info");
			$member_info = BoxModel("yz_member")->where(array('user_id' => $user_info['user_id']))->find();
			//$this->wechatObj->sendText($openid, "has start");
			if ($member_info) {
				//$this->wechatObj->sendText($openid, "has member_info");
				BoxModel("yz_member")->where(array('user_id' => $user_info['user_id']))->save(array('pid' => $keyName));
			} else {
				BoxModel("yz_member")->add(array('pid' => $keyName, 'user_id' => $user_info['user_id'], 'add_time' => time()));
			}

			$content = (string) $user_info['nickname'] . " 通过你的专属二维码关注了【" . (string) $this->storeInfo['app_name'] . "】";
			if ($p_user_info['openid']) {
				$this->wechatObj->sendText($p_user_info['openid'], $content);
			}
		}
	}
}
